package com.lightboat.framework.config;

import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Description: TODO 自定义Jackson
 * @Package: com.mz.config
 * @ClassName: JacksonConfig
 * @Author: 小政同学  Mail：it_xiaozheng@163.com
 * @date: 2022/8/31 15:32
 */
@Configuration
public class JacksonConfig {

    /**
     * 自定义序列化类型转换
     * 注：此处解决Long型转换后，前端js损失精度的问题，将Long型转换为字符串类型
     */
    @Bean
    public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
        // 自定义序列化规则
        return jacksonObjectMapperBuilder -> {
            jacksonObjectMapperBuilder.serializerByType(Long.class, ToStringSerializer.instance);
            jacksonObjectMapperBuilder.serializerByType(Long.TYPE, ToStringSerializer.instance);
        };
    }
}